package asu.logging.targets
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.XMLSocket;

	public class SocketTarget extends LineFormattedTarget
	{
		private var _socket : XMLSocket;
		
		public function SocketTarget( ip:String="127.0.0.1")
		{
			super();
			
			//"10.0.1.199"
			
			_socket = new XMLSocket;
			_socket.addEventListener(Event.CONNECT,onConnect);
			_socket.addEventListener(IOErrorEvent.IO_ERROR,onError);
			_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onError);
			
			try 
			{
				_socket.connect(ip, 4444);
			}
			catch( error:SecurityError )
			{
				trace("SecurityError in SOSAppender: " + error);
			}
		}
		
		private function onError(event:Event):void 
		{
			trace("Error in SOSAppender: " + event);
		}
		
		private function onConnect(event:Event):void 
		{
			_socket.send("Connected\n");
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 *  @private
		 *  This method outputs the specified message directly to 
		 *  <code>trace()</code>.
		 *  All output will be directed to flashlog.txt by default.
		 *
		 *  @param message String containing preprocessed log message which may
		 *  include time, date, category, etc. based on property settings,
		 *  such as <code>includeDate</code>, <code>includeCategory</code>, etc.
		 */
		override protected function internalLog(message:String):void
		{
			if( _socket.connected )
			{
				_socket.send(message+"\n");
			}
		}
	}
}